/*
 *	Source: http://www.yolinux.com/TUTORIALS/XML-Xerces-C.html
 */

#include <string>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <list>


#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>

#include "XMLParser.h"



using namespace xercesc;
using namespace std;


/**
 *  - Tests the access and availability of the XML configuration file.
 *  - Configures the xerces-c DOM parser.
 *  - Reads and extracts the pertinent information from the XML config file.
 * @param const char * configFile - filename
 * @param std::vector<Monster*>& vecResult - information is stored here
 * @return void
 */
void XMLParser::readMonsterConfigFile(const char * configFile, vector<Monster*>& vecResult)
{
	try
	{
		if (checkFile(configFile) == -1)
		{
			throw(std::runtime_error( "XML document not available" ));
		}
		m_ConfigFileParser->parse( configFile );
		DOMDocument* xmlDoc = m_ConfigFileParser->getDocument();
		DOMElement* elementRoot = xmlDoc->getDocumentElement();
		if( !elementRoot )
		{
			throw(std::runtime_error( "empty XML document" ));
		}
		if (!XMLString::equals(elementRoot->getTagName(), m_TAG_root))
		{
			throw(std::runtime_error( "Bad root tag for this config file." ));
		}
		DOMNodeList* childrens = elementRoot->getElementsByTagName(m_TAG_ApplicationSettings);
		const  XMLSize_t nodeCount = childrens->getLength();
		for( XMLSize_t i = 0; i < nodeCount; ++i )
		{
			DOMElement* currentElement = dynamic_cast< xercesc::DOMElement* >(childrens->item(i));

			if (m_ATTR_Options.size() == XMLPARSER_NUMOPTIONS_MONSTERCONFIG)
			{
				Monster* monster = new Monster;
				const XMLCh* xmlch_OptionA = currentElement->getAttribute(m_ATTR_Options[0]);
				const XMLCh* xmlch_OptionB = currentElement->getAttribute(m_ATTR_Options[1]);
				const XMLCh* xmlch_OptionC = currentElement->getAttribute(m_ATTR_Options[2]);
				const XMLCh* xmlch_OptionD = currentElement->getAttribute(m_ATTR_Options[3]);
				const XMLCh* xmlch_OptionE = currentElement->getAttribute(m_ATTR_Options[4]);
				const XMLCh* xmlch_OptionF = currentElement->getAttribute(m_ATTR_Options[5]);
				const XMLCh* xmlch_OptionG = currentElement->getAttribute(m_ATTR_Options[6]);
				const XMLCh* xmlch_OptionH = currentElement->getAttribute(m_ATTR_Options[7]);
				const XMLCh* xmlch_OptionI = currentElement->getAttribute(m_ATTR_Options[8]);
				const XMLCh* xmlch_OptionJ = currentElement->getAttribute(m_ATTR_Options[9]);
				const XMLCh* xmlch_OptionK = currentElement->getAttribute(m_ATTR_Options[10]);

				monster->setName(XMLString::transcode(xmlch_OptionA));
				monster->setXP(atoi(XMLString::transcode(xmlch_OptionB)));
				monster->set_gold_min(atoi(XMLString::transcode(xmlch_OptionC)));
				monster->set_gold_max(atoi(XMLString::transcode(xmlch_OptionD)));
				monster->set_hp_min(atoi(XMLString::transcode(xmlch_OptionE)));
				monster->set_hp_max(atoi(XMLString::transcode(xmlch_OptionF)));
				monster->setPhysicalCombatAttr(atoi(XMLString::transcode(xmlch_OptionG)),
											   atoi(XMLString::transcode(xmlch_OptionH)));
				monster->setMagicalCombatAttr(atoi(XMLString::transcode(xmlch_OptionI)),
											  atoi(XMLString::transcode(xmlch_OptionJ)));
				monster->setEvasion(atoi(XMLString::transcode(xmlch_OptionK)));
				vecResult.push_back(monster);
			}
		}
	}
	catch( xercesc::XMLException& e )
	{
		char* message = xercesc::XMLString::transcode( e.getMessage() );
		ostringstream errBuf;
		errBuf << "Error parsing file: " << message << flush;
		XMLString::release( &message );
	}
}
